﻿using System;
using System.Collections.Generic;
using System.Linq;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Models;
using FoodRaidersWebAPI.Service.Second;

namespace FoodRaidersWebAPI.Service.First
{
    public class MerchantArticleService
    {
        private readonly FoodContext context;
        private readonly ArticleService articleService;
        private readonly MerchantService merchantService;

        public MerchantArticleService(FoodContext context)
        {
            this.context = context;
            articleService = new ArticleService(context);
            merchantService = new MerchantService(context);
        }

        //增加 商家分享
        public void addMerchantArticle(MerchantArticle merchantArticle)
        {
            context.MerchantArticles.Add(merchantArticle);
            context.SaveChanges();
        }
        //删除 商家分享
        public MerchantArticle delete(int id)
        {
            var merchantArticle = findByID(id);
            context.MerchantArticles.Remove(merchantArticle);
            context.SaveChanges();
            merchantArticle.Article = articleService.delete(merchantArticle.ArticleID);
            merchantArticle.Merchant = merchantService.delete(merchantArticle.MerchantID);
            return merchantArticle;
        }

        //id查询   对象不加载
        public MerchantArticle findByID(int id)
        {
            return context.MerchantArticles.FirstOrDefault(m => m.MerchantArticleID == id);
        }
        //id 查询 不加载评论（用户）
        public MerchantArticle findByIDNoCommentLoad(int id)
        {
            var merchantArticle = findByID(id);
            merchantArticle.Article=articleService.findByIDNoCommentLoad(merchantArticle.ArticleID);
            merchantArticle.Merchant = merchantService.findByIDFullLoad(merchantArticle.MerchantID);
            return merchantArticle;
        }
        //id 查询 全加载
        public MerchantArticle findByIDFullLoad(int id)
        {
            var merchantArticle = findByID(id);
            merchantArticle.Article=articleService.findByIDFullLoad(merchantArticle.ArticleID);
            merchantArticle.Merchant = merchantService.findByIDFullLoad(merchantArticle.MerchantID);
            return merchantArticle;
        }
        //id的收藏数+1
        public void addFavorite(int id)
        {
            var merchantArticle = context.MerchantArticles.FirstOrDefault(m => m.MerchantArticleID == id);
            articleService.addFavorite(merchantArticle.ArticleID);
        }
        //id的收藏数-1
        public void subFavorite(int id)
        {
            var merchantArticle = context.MerchantArticles.FirstOrDefault(m => m.MerchantArticleID == id);
            articleService.subFavorite(merchantArticle.ArticleID);
        }
        //id的点赞数+1
        public void addPraise(int id)
        {
            var merchantArticle = context.MerchantArticles.FirstOrDefault(m => m.MerchantArticleID == id);
            articleService.addPraise(merchantArticle.ArticleID);
        }
        //id的点赞数-1
        public void subPraise(int id)
        {
            var merchantArticle = context.MerchantArticles.FirstOrDefault(m => m.MerchantArticleID == id);
            articleService.subPraise(merchantArticle.ArticleID);
        }
        //返回作者id
        public int authorID(int id)
        {
            return findByID(id).UserID;
        }
        //添加评论
        public void addComment(int mid, int uid, string content)
        {
            var merchantArticle = findByID(mid);
            articleService.addComment(merchantArticle.ArticleID,uid,content);
        }
        //返回按点赞数降序的前n位商家分享 不加载评论
        public List<MerchantArticle> ListByPraise(int n)
        {
            var contextMerchantArticles = context.MerchantArticles.ToList();
            foreach (var merchantArticle in contextMerchantArticles)
            {
                merchantArticle.Article = articleService.findByIDNoCommentLoad(merchantArticle.ArticleID);
            }
            var list=contextMerchantArticles.OrderByDescending(m => m.Article.PraiseNumber).Take(n).ToList();
            list.ForEach(l =>
            {
                l.Merchant=merchantService.findByIDFullLoad(l.MerchantID);
            });
            return list;
        }
        //返回按发布时间降序的前n位商家分享 不加载评论
        public List<MerchantArticle> ListByDateTime(int n)
        {
            var contextMerchantArticles = context.MerchantArticles.ToList();
            foreach (var merchantArticle in contextMerchantArticles)
            {
                merchantArticle.Article = articleService.findByIDNoCommentLoad(merchantArticle.ArticleID);
            }
            var list=contextMerchantArticles.OrderByDescending(m => m.Article.DateTime).Take(n).ToList();
            list.ForEach(l =>
            {
                l.Merchant=merchantService.findByIDFullLoad(l.MerchantID);
            });
            return list;
        }
        
        //返回按人均价格升序的前n位商家分享 不加载评论
        public List<MerchantArticle> ListByPrice(int n)
        {
            var contextMerchantArticles = context.MerchantArticles.ToList();
            foreach (var merchantArticle in contextMerchantArticles)
            {
                merchantArticle.Merchant = merchantService.findByIDFullLoad(merchantArticle.MerchantID);
            }
            var list=contextMerchantArticles.OrderByDescending(m => m.Merchant.Price).Take(n).ToList();
            list.ForEach(l =>
            {
                l.Article=articleService.findByIDNoCommentLoad(l.ArticleID);
            });
            return list;
        }
        //返回所有<商家对象>对象{id，经纬度，名字}列表
        public List<MerchantArticle> location()
        {
            var merchantArticles = context.MerchantArticles.ToList();
            merchantArticles.ForEach(m =>
            {
                m.Merchant = merchantService.findByID(m.MerchantID);
            });
            return merchantArticles;
        }
        
    }
}